#!/bin/bash
# 本脚本以 centos7 为基础镜像，在镜像内编译安装 nginx，构建 nginx 容器镜像

#1-4# 定义文本输出颜色
logW() { echo -e "\e[1m $1 \e[0m";  }		#白色
logR() { echo -e "\e[31m $1 \e[0m"; }		#红色
logG() { echo -e "\e[32m $1 \e[0m"; }		#绿色
logY() { echo -e "\e[33m  [$USER@$HOSTNAME] $1 \e[0m"; sleep 1; }	#黄色

logW "1. 创建并进入目录，此目录为构建镜像的上下文"
logY "mkdir -p nginx_build_container && cd nginx_build_container"
mkdir -p nginx_build_container && cd nginx_build_container && logG "success ↑↑↑"
echo

logW "2. 生成构建镜像的 Dockfile 文件"
logY "cat >Dockerfile <<-EOF"
cat >Dockerfile <<-EOF
FROM 180.76.232.94:5000/centos:7.6.1810
MAINTAINER makai "405946301@qq.com"
ENV REFRESHED_AT 2020-6-30
ADD nginx_install.sh /tmp/
ADD public_functions /tmp/
RUN bash -x /tmp/nginx_install.sh
ADD https://cn.wordpress.org/latest-zh_CN.tar.gz /usr/local/nginx/html/
WORKDIR /usr/local/nginx/html/
RUN tar -xf latest-zh_CN.tar.gz
RUN rm -f latest-zh_CN.tar.gz
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
EXPOSE 80
EXPOSE 8080
EOF
echo

logW "3. 生成安装脚本 nginx_install.sh"
logY "cat >nginx_install.sh <<-\"EOF\""
cat >nginx_install.sh <<-"EOF"
#!/bin/bash
# install nginx
. ./tmp/public_functions
checkVersion && clear

readonly nginx_Version="nginx-1.20.2.tar.gz"	# nginx压缩包名称
readonly nginx_Catalog="nginx-1.20.2"		# 压缩包解压后的目录名称
readonly nginx_Module="--user=nginx \
  --prefix=/usr/local/nginx \
  --with-http_ssl_module --with-select_module"  # nginx即将安装的模块

logW "1. 开始安装依赖包"
logY "yum -y install gcc make prce-devel openssl-devel"
yum -y install gcc make prce-devel openssl-devel
checkTF

logW "2. 开始下载 $nginx_Version"
logY "curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version"
curl -# --output /tmp/$nginx_Version https://nginx.org/download/$nginx_Version
checkTF

logW "3. 开始解压 $nginx_Version"
logY "tar -xvf /tmp/$nginx_Version -C /tmp"
tar -xvf /tmp/$nginx_Version -C /tmp
checkTF

logW "4. 进入nginx安装包目录 /tmp/$nginx_Catalog"
logY "cd /tmp/$nginx_Catalog"
cd /tmp/$nginx_Catalog
checkTF

logW "5. 开始编译并安装 $nginx_Version"
logY "./configure $nginx_Module && make && make install"
./configure $nginx_Module && make && make install
checkTF

logW "6. 添加用户 nginx"
logY "useradd -M -s /sbin/nologin nginx"
useradd -M -s /sbin/nologin nginx
if [[ $? -eq 0 || $? -eq 9 ]]; then
    logG "success ↑↑↑"
    echo " "
  else
    logR "fail ↑↑↑       -->> return $?"
    exit 0
  fi

logW "7. 显示nginx信息"
logY "/usr/local/nginx/sbin/nginx -V  && netstat -tupln | grep Proto && netstat -tupln | grep nginx"
/usr/local/nginx/sbin/nginx -V
checkTF

logW "8. 放行80端口"
getFirewalldStatus
if [[ $? -eq 0 ]]; then
  logY "firewall-cmd --zone=public --add-port="80"/"tcp" --permanent"
  firewall-cmd --zone=public --add-port="80"/"tcp" --permanent 2>/dev/null
  firewall-cmd --reload &>/dev/null
  echo ""
else
  logR "防火墙未开启"
  echo ""
fi

rm -rf /tmp/"$nginx_Version" /tmp/"$nginx_Catalog"	# 删除nginx软件和解压后的目录
rm -rf /tmp/nginx_install.sh /tmp/public_functions
EOF
echo

null1() {
logW "4. 生成下载wordpress脚本文件"
logY "cat >wordpress_download.sh <<-EOF"
cat >wordpress_download.sh <<-EOF
#!/bin/bash
# download wordpress
. ./tmp/public_functions

logW "1. 切换目录至 /usr/local/nginx/html/"
logY "cd /usr/local/nginx/html/"
cd /usr/local/nginx/html/
checkTF

logW "2. 开始下载wordpress"
logY "curl -# --remote-name https://cn.wordpress.org/latest-zh_CN.zip"
curl -# -O https://cn.wordpress.org/latest-zh_CN.zip

logW "3. 开始解压wordpress"
logY "unzip latest-zh_CN.zip"
unzip latest-zh_CN.zip
checkTF

logW "4. 删除wordpressi压缩包"
logY "rm -f latest-zh_CN.zip"
rm -f latest-zh_CN.zip
checkTF
EOF
}

logW "4. 生成函数库 public_functions"
logY "cat >public_functions <<-\"EOF\""
cat >public_functions <<-"EOF"
# This is script functions
# 这是公共函数，共14个
# logW() logR() logG() logY() logP() checkTF() execTips()
# 8.getFirewalldStatus() 9.enableOrDisEnableFirewalld() 10.checkVersion()
# 11.add_docker_repo() 12.enable_Selinux() 13.disable_Selinux() 14.selinux_Menu()

#1-5# 定义文本输出颜色
logW() { echo -e "\e[1m $1 \e[0m"; }		#白色
logR() { echo -e "\e[31m $1 \e[0m"; }		#红色
logG() { echo -e "\e[32m $1 \e[0m"; }		#绿色
logY() { echo -e "\e[33m  [$USER@$HOSTNAME] $1 \e[0m"; sleep 1; }		#黄色
logP() { echo -en "\e[35m $1 \e[0m"; }		#紫色

#6# 检查命令执行是否成功
checkTF() {
  if [[ $? -eq 0 ]]; then
    logG "success ↑↑↑" && echo
  else
    logR "fail ↑↑↑      -->> return $?" && exit 0
  fi
  sleep 2
}

#7# 定义输入选择，输入y返回0，输入n返回1
execTips() {
  logP "$1 [y/n]":
  read input

  case $input in
    [yY][eE][sS] | [yY])
      return 0 ;;
    [nN][oO] | [nN])
      exit 1 ;;
    *)
      logR "输入不合法，请重新输入" ;;
  esac
}

#8# 查询防火墙状态，开启返回1，关闭返回0
getFirewalldStatus() {
  firewall-cmd --state &> /dev/null

  if [[ $? -eq 0 ]]; then
    return 0
  else
    return 252
  fi
}

#9# 开启或关闭防火墙
enableOrDisEnableFirewalld() {
  getFirewalldStatus
  if [[ $? -eq 0 ]]; then
    logP "检测到防火墙开启状态，是否关闭? 输入Y关闭,输入N取消 [y/n]:"
    read -r isClose

    case $isClose in
      [yY][eE][sS] | [yY])
        systemctl disable --now firewalld
        if [[ $? -eq 0 ]]; then
          logG "防火墙关闭成功！"
        else
          logR "防火墙关闭失败！"
        fi ;;
      *)
        logR "输入不合法，请重新输入";;
    esac

  else
    logP "检测到防火墙关闭状态，是否开启? 输入Y开启,输入N取消 [y/n]:"
    read -r isOpen

    case $isOpen in
      [yY][eE][sS] | [yY])
        systemctl enable --now firewalld
        if [[ $? -eq 0 ]]; then
          logG "防火墙开启成功！"
        else
          logR "防火墙开启失败！"
        fi ;;
      *)
        logR "输入不合法，请重新输入";;
    esac

  fi
}

#10# 检查系统版本
checkVersion() {
  system_version=`rpm -q centos-release | cut -d- -f3`
  
  if [ $system_version -ne 7 ]; then
      logR "Sorry~ 仅支持CentOS7.x的系统使用"
      exit 1
  fi
}

#11# 添加docker yum源
add_docker_repo() {
  logW "1. 添加docker的yum源"
  logY "cat >/etc/yum.repos.d/docker-ce.repo <<-BOF..."
  cat <<BOF >/etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=0
gpgkey=https://download.docker.com/linux/centos/gpg
BOF
  yum repolist | grep docker-ce
  checkTF
}

#12# 开启selinux
enable_Selinux() {
  selinuxStatus=`getenforce`
  sed -i "s/SELINUX=(permissive|disabled)/SELINUX=enforcing/" /etc/selinux/config
  if [ $selinuxStatus = 'Enforcing' ]; then
    logY "selinux已经是开启状态"
    logW "配置文件状态为: `cat /etc/selinux/config | grep "^SELINUX=$*"`"
  elif [ $selinuxStatus = 'permissive' ]; then
    setenforce 1
    logY "selinux开启成功"
    logW "配置文件更改为: `cat /etc/selinux/config | grep "^SELINUX=$*"`"
  else
    logY "selinux开启成功"
    logW "配置文件已更改为：`cat /etc/selinux/config | grep "^SELINUX=$*"` 重启系统后生效!"
  fi
}

#13# 关闭selinux
disable_Selinux() {
  selinuxStatus=`getenforce`
  sed -Ei "s/SELINUX=(enforcing|permissive)/SELINUX=disabled/g" /etc/selinux/config
  if [ $selinuxStatus = 'Disabled' ]; then
    logY "selinux已经是关闭状态"
    logW "配置文件状态为: `cat /etc/selinux/config | grep "^SELINUX=$*"`"
  else
    setenforce 0
    logW "配置文件已更改为：`cat /etc/selinux/config | grep "^SELINUX=$*"`"
  fi
}

#14# selinux菜单
selinux_Menu() {
  selinuxStatus=`getenforce` && clear

  logW " ---------------------------------
  当前selinux状态是: $selinuxStatus
  ---------------------------------
  1) 开启selinux  2) 关闭selinux
  ---------------------------------"

  logP "请输入有效的操作编号(1-2):"
  read tipsNum

  case $tipsNum in

    1)
      enable_Selinux && echo "" ;;
    2)
      disable_Selinux && echo "" ;;
    *)
      selinux_Start ;;
  esac
}
EOF
echo

logW "5. 开始构建镜像 makai/centos-nginx:1.20.2"
logY "docker build --tag makai/centos-nginx:1.20.2 ."
docker build --tag makai/centos-nginx:1.20.2 .
echo

cd .. && rm -rf nginx_build_container
logG "####程序共执行了 $SECONDS秒 ####"
